﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ELearning.Wpf.UI
{
    using ELearning.Data.LearningDatasetTableAdapters;

    /// <summary>
    /// Interaction logic for wndTestViewer.xaml
    /// </summary>
    public partial class wndTestViewer : Window
    {
        private Test _currentTest;
        
        private Question _currentQuestion;

        public wndTestViewer() : this(1){}
        public wndTestViewer(long testId)
        {
            InitializeComponent();

            using (TestsTableAdapter tableAdapter = new TestsTableAdapter())
            {
                var dataTable = tableAdapter.GetDataByID(testId);

                if (dataTable.Count <= 0)
                {
                    string message = "Couldn't find test!";
                    DebugHelper.ShowDebugMessageBox(message);
                    throw new Exception(message);
                }

                _currentTest = wndCreateTest.LoadTestFromByteArray(dataTable[0].BinaryData);
                if (_currentTest.Questions == null || _currentTest.Questions.Count == 0)
                {
                    string message = "No questions in test!";
                    DebugHelper.ShowDebugMessageBox(message);
                    throw new Exception(message);
                }

                _currentQuestion = _currentTest.Questions[0];            
            }

            Bind();
        }

        private void Bind()
        {
            lbQuestionText.Content = _currentQuestion.QuestionText;
            lbCounter.Content = string.Format("{0}/{1}", GetCurrentQuestionIndex() + 1, _currentTest.Questions.Count);
            lbAnswers.DataContext = null;
            lbAnswers.DataContext = _currentQuestion.Answers;
        }

        private int GetCurrentQuestionIndex()
        {
            return _currentTest.Questions.IndexOf(_currentQuestion);
        }

        private void btNext_Click(object sender, RoutedEventArgs e)
        {
            int currentIndex = GetCurrentQuestionIndex();

            if (currentIndex < _currentTest.Questions.Count - 1)
            {
                _currentQuestion = _currentTest.Questions[currentIndex + 1];
                Bind();
            }
            else
            {
                CalculateResults();
            }
        }

        private void CalculateResults()
        {
            int total = 0;
            int result = 0;

            foreach (var question in _currentTest.Questions)
            {
                foreach(var answer in question.Answers)
                {

                    if (answer.IsCorrect)
                    {
                        total++;
                    }

                    if (answer.IsChecked && answer.IsCorrect)
                    {
                        result++;
                    }
                    if (answer.IsChecked && !answer.IsCorrect)
                    {
                        result--;
                    }

                }
            }

            MessageBox.Show(string.Format("Загалом:{0}\nРезультат{0}", total, result));
        }
    }
}
